package tablet.module_user.activity;

import android.content.Context;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.lib.app.ARouterPathUtils;
import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.views.load_state_view.MultiStateView;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.fastkit.views.spring_refresh.container.DefaultFooter;
import com.lib.fastkit.views.spring_refresh.container.DefaultHeader;
import com.lib.fastkit.views.spring_refresh.widget.SpringView;
import com.lib.http.call_back.HttpNormalCallBack;
import com.lib.ui.activity.BaseAppActivity;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.utls.glide.GlideConfig;
import com.lib.view.navigationbar.NomalNavigationBar;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import tablet.module_user.R;
import tablet.module_user.R2;
import tablet.module_user.bean.OrderListBean;

/**
 * Created by Administrator on 2020/7/30 0030.
 */

@Route(path = ARouterPathUtils.Tablet_User_OrderListActivity)
public class OrderListActivity extends BaseAppActivity {

    @BindView(R2.id.springView)
    SpringView springView;
    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.state_view)
    MultiStateView stateView;

    HomeAdapter homeAdapter;
    private List<OrderListBean.ObjBean.RowsBean> list = new ArrayList<>();

    @Override
    protected void onCreateView() {

        stateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
        stateView.setMultiStateViewLisener(new MultiStateView.MultiStateViewLisener() {
            @Override
            public void onTryAgain() {
                initData();
            }
        });

        initTitle();
        initView();

        initData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_orderlist;
    }

    private int page = 0;

    private void initData() {

        HttpUtils.with(this)
                .addParam("requestType", "ONLINE_ORDER_RECORD_LIST")
                .addParam("token", SharedPreferenceManager.getInstance(this).getUserCache().getUserToken())
                .addParam("page", page)
                .addParam("limit", "10")
                .execute(new HttpNormalCallBack<OrderListBean>() {
                    @Override
                    public void onSuccess(OrderListBean result) {

                        stateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        if (result.getCode() == CodeUtil.CODE_200) {

                            if (result.getObj() != null) {

                                if (page == 0) {
                                    if (list.size() > 0) {
                                        list.clear();
                                    }
                                }
                                list.addAll(result.getObj().getRows());
                                homeAdapter.notifyDataSetChanged();
                            }

                        } else {
                            stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        }

                    }

                    @Override
                    public void onError(String e) {
                        if (stateView != null) {
                            stateView.setViewState(MultiStateView.VIEW_STATE_NETWORK_ERROR);
                        }
                    }
                });

    }


    private void initView() {

        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));

        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                initData();

                springView.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {

                ++page;
                initData();

                springView.onFinishFreshAndLoad();
            }
        });

        homeAdapter = new HomeAdapter(this, list);
        rvList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return true;
            }
        });
        rvList.setAdapter(homeAdapter);

    }

    protected void initTitle() {
        NomalNavigationBar navigationBar = new
                NomalNavigationBar.Builder(this)
                .setTitle("音视频答疑订单")
                .builder();


    }


    private class HomeAdapter extends BaseAdapter<OrderListBean.ObjBean.RowsBean> {

        public HomeAdapter(Context context, List<OrderListBean.ObjBean.RowsBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_orderlist;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<OrderListBean.ObjBean.RowsBean> mData) {

            OrderListBean.ObjBean.RowsBean rowsBean = mData.get(position);

            ImageView ivTeacher = holder.getView(R.id.iv_user);
            if (StringUtil.isNotEmpty(rowsBean.getPersonal_cover(), true)) {
                Glide.with(OrderListActivity.this)
                        .load(rowsBean.getPersonal_cover())
                        .apply(GlideConfig.getRoundOptions(10))
                        .into(ivTeacher);
            }

            if (StringUtil.isNotEmpty(rowsBean.getTeacher_name(), true)) {
                holder.setText(R.id.tv_name, rowsBean.getTeacher_name());
            }
            if (StringUtil.isNotEmpty(rowsBean.getGrade_name(), true)) {
                holder.setText(R.id.tv_grade, rowsBean.getGrade_name());
            }

            if (StringUtil.isNotEmpty(rowsBean.getSubject_name(), true)) {
                holder.setText(R.id.tv_subject, rowsBean.getSubject_name());
            }

            if (StringUtil.isNotEmpty(rowsBean.getLive_duration(), true)) {
                holder.setText(R.id.tv_time, rowsBean.getLive_duration());
            }

            if (StringUtil.isNotEmpty(rowsBean.getCreation_date(), true)) {
                holder.setText(R.id.tv_date, rowsBean.getCreation_date());
            }
        }
    }

}
